package net.qunzhi.modules.cms.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.qunzhi.common.utils.BaseQuery;
import net.qunzhi.common.annotation.SysLog;
import net.qunzhi.common.utils.Constant;
import net.qunzhi.modules.cms.query.CompletionData;
import net.qunzhi.modules.cms.service.TbFlowMeterService;
import net.qunzhi.modules.cms.service.TbFlowStateService;
import net.qunzhi.modules.cms.service.TbPressureStateService;
import net.qunzhi.modules.historyspider.SpiderTimer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import net.qunzhi.modules.cms.entity.TbEquipmentEntity;
import net.qunzhi.modules.cms.service.TbEquipmentService;
import net.qunzhi.common.utils.PageUtils;
import net.qunzhi.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 设备信息
 *
 * @author zjr
 * @date 2021-09-10 15:48:00
 */
@Api(tags = "设备信息相关")
@RestController
@RequestMapping("cms/tbEquipment")
public class TbEquipmentController {
    @Autowired
    private TbEquipmentService tbEquipmentService;

    /**
     * 列表
     */
    @ApiOperation("列表")
    @GetMapping("/list")
    @RequiresPermissions("cms:tbEquipment:list")
    public R list(BaseQuery query){
        PageUtils page = tbEquipmentService.queryPage(query);

        return R.ok().put("page", page);
    }

    @ApiOperation("下拉框")
    @GetMapping("/combo")
    public R combo(){
        List<Map<String, Object>> list = tbEquipmentService
                .listMaps(new QueryWrapper<TbEquipmentEntity>().select("id", "name", "flow_meter_name flowMeterName", "type"));

        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @ApiOperation("信息")
    @GetMapping("/info/{id}")
    @RequiresPermissions("cms:tbEquipment:info")
    public R info(@PathVariable("id") Long id){
		TbEquipmentEntity tbEquipment = tbEquipmentService.getById(id);

        return R.ok().put("tbEquipment", tbEquipment);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @SysLog("设备信息保存")
    @PostMapping("/save")
    @RequiresPermissions("cms:tbEquipment:save")
    public R save(@RequestBody TbEquipmentEntity tbEquipment){
		tbEquipmentService.save(tbEquipment);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @SysLog("设备信息修改")
    @PostMapping("/update")
    @RequiresPermissions("cms:tbEquipment:update")
    public R update(@RequestBody TbEquipmentEntity tbEquipment){
		tbEquipmentService.updateById(tbEquipment);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @SysLog("设备信息删除")
    @PostMapping("/delete")
    @RequiresPermissions("cms:tbEquipment:delete")
    public R delete(@RequestBody Long[] ids){
		tbEquipmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    @ApiOperation("一键获取未完全爬取数据")
    @RequestMapping("/getSpiderData")
    public void getSpiderData() {

        tbEquipmentService.getSpiderData();

    }

    @ApiOperation("补齐数据")
    @SysLog("补齐数据")
    @RequestMapping("/completion")
    public R completion(CompletionData data){
        tbEquipmentService.completionData(data);

        return R.ok();
    }

}
